// pages/mine/feedbak/add/add.js
Page({


  data: {  fileList:[],name:'',list:[],res:'',phone:'',introduce:'',address:'',arr2:[],index2:0,arr3:[],index3:0,arr4:[],last:'',bank:'',price:'',bank_id:'',
  number:'',type:'',remark:'',price2:'',bank1:'',code1:'',radio:1,flag:false,flag1:false,yindex:'',
  price1:'',
  select:[false,false,false,false,false,false
  ,false,false,false,false,false,false],totalprice:0

  },

  
  onLoad: function (options) {
      this.getshebei()
      this.getshop()
      this.getlist()
      this.getchaxun()
      this.getyearlist()
  },
  onShow(){
      // this.getlist()
      // this.getchaxun()
    
  },

  
  afterRead(event) {
    const { file } = event.detail;
    console.log(file);
    const that=this
      
    
    
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      
      url: 'https://zujieba.com/index/upload/upload', // 仅为示例，非真实的接口地址
      filePath: file.path,
      name:'file',
      
      success(res) {
        
        const res1=JSON.parse(res.data).name
        const array=that.data.list
        array.push(res1)
     if(!that.data.res){
       var res=  that.data.res+res1}else{
        var res=  that.data.res+","+res1
       }
      
        that.setData({
          list:array,
         res
        })
        
        
        
        
        console.log(that.data.res);
        const url1="https://zujieba.com/file_index/"+res1
        
        console.log(url1);
        
        const { fileList = [] } = that.data;
        const object={}
        object.url=url1
        object.name=2
        object.deletable=true
        
       fileList.push(object) 
        that.setData({ fileList });
      }
      
    });
  },
  deleteclick2(e){
    const index=  e.detail.index
    const list=this.data.fileList
        list.splice(index,1)
       this.setData({
         fileList:list
       })
           
         
    },
    change2(e){
      this.setData({
        name:e.detail.value
      })
    },
    change3(e){
       this.setData({
         phone:e.detail.value
       })
    },
    change4(e){
      this.setData({
        address:e.detail.value   
      })
    },
  
    change5(e){
      this.setData({
        introduce:e.detail.value
      })
    },
  
      submit(){
     
        if(this.data.bank==''){
        return   wx.showToast({
            title: '请绑定提现的银行卡',
            icon:'none'
          })
        }
        if(this.data.totalprice==''||this.data.totalprice==0){
          return   wx.showToast({
              title: '请输入提现余额',
              icon:'none' 
            })
          }
        
        
       
        let  pay=this.data.radio
        const file=this.data.list
        const uid=wx.getStorageSync('uid')
        const code=this.data.bank.id
        const price=this.data.totalprice
        let year=this.data.yearlist[this.data.yindex]
        let str=''
      //  计算月份选中的月份
      this.data.select.forEach((item,index)=>{
          if(item==true){
            if(index+1<10){
              str+=year+'-'+'0'+(index+1)+','
            }else{
              str+=year+'-'+(index+1)+','
            }
          }
      })
        
           const params={uid,file,code,price,pay,date:str}  
           console.log(params);
           
          wx.request({
            url: 'https://zujieba.com/api/money/cash.html',
            data:params,
            method:'POST',
            success:(res)=>{
              console.log(res);
              if(res.data.success==1){
                wx.showToast({
                  title: res.data.tips,
                  icon:'none'
                })
                setTimeout(()=>{
                  this.throttle(()=>{
                   wx.navigateBack({
                     detail:1,
                     url: '/pages/mine/store_admin/index',
                     success:(res)=>{
                        
                     }
                   })
                  },1500)
                 },2000)
              
              }else{
                console.log(2);
                
                wx.showToast({
                  title: res.data.tips,
                  icon:'none',
                  duration:2000,
                  
                })
                setTimeout(()=>{
                 this.throttle(()=>{
                  // wx.navigateBack({
                  //   detail:1,
                  //   url: '/pages/mine/store_admin/index',
                  //   success:(res)=>{
                       
                  //   }
                  // })
                 },1500)
                },2000)
                
              }


              
            }
          })
          
        
      },
      bindPickerChange2(e){
         this.setData({
           index2:e.detail.value
         })
      },
      bindPickerChange3(e){
        this.setData({
          index3:e.detail.value
        })
     },
      getshebei(){
         wx.request({
           url: 'https://zujieba.com/Api/feedback/laber.html',
           method:'POST',
           success:(res)=>{
             console.log(res);
           let list= res.data.data
           let arr=[]
           list.forEach((item)=>{
                 arr.push(item.title)
           })
           this.setData({
             arr2:arr
           })
           
             
           }
         })
      },
      getshop(){
        wx.request({
          url: 'https://zujieba.com/Api/feedback/store.html',
          method:'POST',
          data:{uid:wx.getStorageSync('uid')},
          success:(res)=>{
            console.log(res);
          let store=res.data.data
          let arr4=[]
          let arr1=[]
          store.forEach((item)=>{
              arr1.push(item.title)
              arr4.push(item.id)
          })
          this.setData({
             arr3:arr1,
             arr4
          })
            
          }
        })
      },
      //节流
    throttle:function(func,wait){
      console.log(func,wait);
      
      let last =this.data.last
      console.log(last);
      
      let current_time= +new Date()
      if(current_time-last>wait){
        console.log(1111);
          console.log(current_time,current_time-last);
          
          func()
          last=+new Date()
          this.setData({
            last
          })
      }
        
         
      
       
    },
//跳转到添加银行卡页面
navyinghang(){
   wx.navigateTo({
     url: '/pages/mine/store_admin/addyinghang',
   })
},
address_save(){
  wx.navigateTo({
    url: '/pages/mine/store_admin/success',
  })
},
//拿到银行卡图标
getlist(){
  wx.request({
    url: 'https://zujieba.com/Api/money/show.html',
    data:{uid:wx.getStorageSync('uid'),bank_id:this.data.bank_id},
    method:'POST',
    success:(res)=>{
      console.log(res);
      if(res.data.success==1){
           this.setData({
             price:res.data.data.total,
             bank:res.data.data.bank,
             number:res.data.data.number
           })
      }
      
    }
  })
},
change1(e){
    this.setData({price1:e.detail.value})
},
tixian(){
  wx.navigateTo({
    url: '/pages/mine/recoed/record',
  })
},
getchaxun(){
  wx.request({
    url: 'https://zujieba.com/Api/money/show_cash.html',
    data:{uid:wx.getStorageSync('uid')},
    method:"POST",
    success:(res)=>{
       console.log(res);
       if(res.data.success==1){
        this.setData({type:res.data.data.type,remark:res.data.data.remark,price2:res.data.data.price,code1:res.data.data.code,bank1:res.data.data.bank})
       }else if(res.data.success==2){
          
       }else{
          this.setData({type:''})
       }
      
       
    }
  })
},
submit1(){
  this.setData({type:''})
},
toggle(e){
  console.log(e);
  
  this.setData({
    radio:e.currentTarget.dataset.radio
  })
},
getyearlist(){
  var date = new Date();
  var year=date.getFullYear()
  console.log(year);
  let list=[year]
  for(var i=0;i<2;i++){
   year--
   list.unshift(year)
   console.log(list);
   
  }
  year=year+2
  for(var i=0;i<2;i++){
    
    year++
    list.push(year)
    console.log(list);
   this.setData({
     yearlist:list
   })
   }
  
},
toggle1(){
  let flag=!this.data.flag
  
  if(flag==true&&this.data.flag1==true){
    let flag1=!this.data.flag1
    this.setData({
       flag1
    })
  }
   this.setData({
      flag
   })
},
toggle2(){
  let flag1=!this.data.flag1
  if(flag1==true&&this.data.flag==true){
    let flag=!this.data.flag
    this.setData({
       flag
    })
  }
   this.setData({
      flag1
      
   })
},
getyear(e){
   this.setData({
     title:this.data.yearlist[e.currentTarget.dataset.i],
     yindex:e.currentTarget.dataset.i
     
   })
   this.geteverymonthmoney(e.currentTarget.dataset.i)
},
getselect(e){
  let flag=this.data.select[e.currentTarget.dataset.i]
  let select=this.data.select
  if(this.data.monthlist1[e.currentTarget.dataset.i]==0){
    return
  }
  //不能提现当月的余额
   var data=new Date()
   var month=data.getMonth()
   console.log(month,e.currentTarget.dataset.i);
   
   if(e.currentTarget.dataset.i==month){
     wx.showToast({
       title: '不能提现当月的余额',
       icon:'none'
     })
     return
   }
  //  jieshu
  select[e.currentTarget.dataset.i]=!flag
  this.setData({
    select
  })
  this.gettixianmoney()
},
// 通过年份获取每月的钱
geteverymonthmoney(yearIndex){
   wx.request({
     url: 'https://zujieba.com/Api/money/month.html',
     data:{uid:wx.getStorageSync('uid'),date:this.data.yearlist[yearIndex]},
     method:'POST',
     success:(res)=>{
       console.log(res);
      //  处理对象数据 变为数组

      
       let arr=[res.data.data.one_price,res.data.data.two_price,res.data.data.three_price,res.data.data.four_price,res.data.data.five_price,res.data.data.six_price,
        res.data.data.seven_price,res.data.data.eight_price,res.data.data.nine_price,res.data.data.ten_price,res.data.data.eleven_price,res.data.data.twelev_price]
        console.log(arr);
       this.setData({
         
         
        monthlist1:arr
       })
     }
   })
},
//计算提现金额
    gettixianmoney(){
      let price=0
    this.data.select.forEach((item,index)=>{
      if(item==true){
        console.log(index);
        price+=this.data.monthlist1[index]
        console.log(price);
        
      }
      console.log(price);
      
      this.setData({
         totalprice:price
      })
    })
    }
 
})